package com.simple.portal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.portal.dao.IGroupRightRelDAO;
import com.simple.portal.vo.GroupRightRelVO;
import com.simple.util.common.Constants;
import com.simple.util.common.StringUtil;

@Repository("groupRightRelDAO")
public class GroupRightRelDAOImpl extends BaseDAO implements IGroupRightRelDAO {

	@Override
	public GroupRightRelVO findById(final String id) {
		
		return (GroupRightRelVO)super.get(GroupRightRelVO.class, id);
	}

	@Override
	public void save(final GroupRightRelVO groupRightRelVO,final String operator) {
		
		GroupRightRelVO tempGroupRightRelVO = super.save(groupRightRelVO);
		super.saveLogger(operator, tempGroupRightRelVO, tempGroupRightRelVO.getGroupId(), Constants.LOGGER_OPERATE_FLAG_ADD);
	}
	
	@Override
	public void update(final GroupRightRelVO groupRightRelVO,final String type,final String operator) {
		
		super.update(groupRightRelVO);
		super.saveLogger(operator, groupRightRelVO, groupRightRelVO.getGroupId(), type);
	}

	@Override
	public List<GroupRightRelVO> findAll() {
		
		return super.query(GroupRightRelVO.class);
	}

	@Override
	public void delete(final GroupRightRelVO groupRightRelVO,final String operator) {
		
		super.delete(groupRightRelVO);
		super.saveLogger(operator, groupRightRelVO, groupRightRelVO.getGroupId(), Constants.LOGGER_OPERATE_FLAG_DELETE);
	}

	@Override
	public void delete(final String id,final String operator) {
		GroupRightRelVO groupRightRelVO = findById(id);
		super.delete(GroupRightRelVO.class, id);
		super.saveLogger(operator, groupRightRelVO, groupRightRelVO.getGroupId(), Constants.LOGGER_OPERATE_FLAG_DELETE);
	}
	
	@Override
	public List <GroupRightRelVO> findAllByGroupIdAndRightFlag(final String groupId, final String rightFlag){
		final List<String> paramList = new ArrayList<String>();
		paramList.add(groupId);
		final StringBuffer sb = new StringBuffer(300);
		sb.append( "select grr  from GroupRightRelVO  grr  where  grr.groupId=?");
		if(!StringUtil.isNull(rightFlag)){
			paramList.add(rightFlag);
			sb.append(" and grr.rightFlag = ?");
		}
		return super.query(sb.toString(),paramList);
	}
	
}
